package com.shiku.imserver.common.scanner;

import com.shiku.imserver.common.constant.IMLoggers;
import java.io.File;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.slf4j.Logger;
import sun.net.www.protocol.file.FileURLConnection;

public class PackageScaner {
    public static final Logger logger = IMLoggers.serverLog;

    public static String[] scanNamespaceFiles(String namespace, String fileext, boolean isReturnCanonicalPath) {
        return scanNamespaceFiles(namespace, fileext, isReturnCanonicalPath, false);
    }

    /* JADX DEBUG: Failed to find minimal casts for resolve overloaded methods, cast all args instead
     method: ClspMth{java.lang.String.replace(char, char):java.lang.String}
     arg types: [int, int]
     candidates:
      ClspMth{java.lang.String.replace(java.lang.CharSequence, java.lang.CharSequence):java.lang.String}
      ClspMth{java.lang.String.replace(char, char):java.lang.String} */
    public static String[] scanNamespaceFiles(String namespace, String fileext, boolean isReturnCanonicalPath, boolean checkSub) {
        URL url;
        String tmpItemName;
        int tmpPos;
        String respath = namespace.replace('.', '/').replace('.', '/');
        List<String> tmpNameList = new ArrayList<>();
        try {
            logger.info("scan url path " + respath);
            if (!respath.startsWith("/")) {
                url = PackageScaner.class.getResource("/" + respath);
            } else {
                url = PackageScaner.class.getResource(respath);
            }
            logger.info("scan url  " + url);
            URLConnection tmpURLConnection = url.openConnection();
            if (tmpURLConnection instanceof JarURLConnection) {
                JarFile jarFile = ((JarURLConnection) tmpURLConnection).getJarFile();
                Enumeration<JarEntry> entrys = jarFile.entries();
                while (entrys.hasMoreElements()) {
                    JarEntry tmpJarEntry = entrys.nextElement();
                    if (!tmpJarEntry.isDirectory()) {
                        String tmpItemName2 = tmpJarEntry.getName();
                        if (tmpItemName2.indexOf(36) < 0 && ((fileext == null || tmpItemName2.endsWith(fileext)) && (tmpPos = tmpItemName2.lastIndexOf(47)) > 0)) {
                            String tmpPath = tmpItemName2.substring(0, tmpPos);
                            if (checkSub) {
                                if (tmpPath.startsWith(respath)) {
                                    tmpNameList.add(tmpItemName2.substring(respath.length() + 1).replaceAll("/", "."));
                                }
                            } else if (respath.equals(tmpPath)) {
                                tmpNameList.add(tmpItemName2.substring(tmpPos + 1));
                            }
                        }
                    }
                }
                jarFile.close();
            } else if (tmpURLConnection instanceof FileURLConnection) {
                File file = new File(url.getFile());
                if (!file.exists() || !file.isDirectory()) {
                    logger.error("scaning stop,invalid package path:" + url.getFile());
                } else {
                    File[] fileArray = file.listFiles();
                    int length = fileArray.length;
                    for (int i = 0; i < length; i++) {
                        File f = fileArray[i];
                        if (!f.isDirectory() || f.getName().indexOf(".") == -1) {
                            if (isReturnCanonicalPath) {
                                tmpItemName = f.getCanonicalPath();
                            } else {
                                tmpItemName = f.getName();
                            }
                            if (f.isDirectory()) {
                                String[] inner = scanNamespaceFiles(namespace + "." + tmpItemName, fileext, isReturnCanonicalPath);
                                if (inner != null) {
                                    int length2 = inner.length;
                                    for (int i2 = 0; i2 < length2; i2++) {
                                        String i3 = inner[i2];
                                        if (i3 != null) {
                                            tmpNameList.add(tmpItemName + "." + i3);
                                        }
                                    }
                                }
                            } else if (fileext == null || tmpItemName.endsWith(fileext)) {
                                tmpNameList.add(tmpItemName);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("scaning stop,invalid package path error" + e.toString());
        }
        if (tmpNameList.size() <= 0) {
            return null;
        }
        String[] r = new String[tmpNameList.size()];
        tmpNameList.toArray(r);
        tmpNameList.clear();
        return r;
    }
}
